<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

/**
*供应商类
*
*/
class SupplierController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    // 供应商列表显示 
    public function getIndex(Request $request)
    {   
        $count = $request -> input('count',1);
        $search = $request -> input('search','');
        $data = DB::table('sa_supplier') -> paginate($count);
        return view('admin.supplier.index',['data'=>$data,'request' => $request -> all()]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    // 供应商添加页面显示
    public function getCreate()
    {
        
        return view('admin.supplier.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    // 创建一个供应商到数据库
    public function postStore(Request $request)
    {
        $arr = array();
        // 获取表单提交的信息
        $post = $request -> except('_token');
        // 整合信息
        $arr['s_name'] = $post['name'];
        $arr['s_industry'] = $post['trades'];
        $arr['s_manager'] = $post['manager'];
        $arr['s_tel'] = $post['tel'];
        $arr['s_area'] = $post['address'];
        // 写入数据库
        $res = DB::table('sa_supplier') -> insert($arr);
        // 判断写入数据库是否成功
        if($res){
            return redirect('admin/supplier/index') ->with('success','添加成功');
        }else{
            return back() -> withInput() -> with('error','添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getEdit(Request $request)
    {
        //
        $spId = $request->get('id');
        $data = DB::table('sa_supplier')->where('s_id','=',$spId)->first();
        return view('admin.supplier.edit',['data'=>$data]);
    }

    //编辑供应商处理
    public function postEditaction(Request $request)
    {
        //获取ajax传参Edit
        $id = $request -> input('id');
        $s_name = $request->input('name');
        $s_industry = $request->input('trades');
        $s_manager = $request->input('manager');
        $s_tel = $request->input('tel');
        $s_area = $request->input('address');
        //修改员工表记录
        $row = DB::table('sa_supplier')->where('s_id','=',$id)->update(compact('s_name','s_industry','s_manager','s_tel','s_area'));
        if($row){
            //修改成功
            return redirect('admin/supplier/index') ->with('sussccess','修改成功');
        }else{
            //修改失败
            return back() -> withInput() -> with('error','修改失败');
        }
        echo json_encode($data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
